﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;

namespace UI.ModSalary
{
    public partial class SalaryTable : System.Web.UI.Page
    {
        string account = "";
        DefDicBiz defDicBiz = new DefDicBiz();
        SalaryTableBiz salaryTableBiz = new SalaryTableBiz();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            gvList.PageSize = XmlUtility.GetNumFormConfig("numOfSal");
            if (!IsPostBack)
            {                
                defDicBiz.InitXMLDropDownList(ddlYear, "YearList.xml", System.DateTime.Now.Year.ToString());
                defDicBiz.InitXMLDropDownList(ddlMonth, "MonthList.xml", (System.DateTime.Now.Month - 1).ToString());
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID, "1");
            }
        }

        DataTable GetSource()
        {
            string empCode = txtSeWord.Text.Trim();//只查询使用
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            string setsID = ddlSetsID.SelectedValue;
            if (string.IsNullOrWhiteSpace(empCode))
            {
                if (setsID == "0")
                {
                    JSUtility.Alert(this, "请先选择帐套");
                    return null;
                }
            }

            return salaryTableBiz.GetLists(empCode, strYear, strMonth, setsID);
        }
        //查询工资簿
        protected void btnEmSearch_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "SalaryTable.aspx", RightChar.Ser))
            {
                JSUtility.Alert(this, "您没有查询工资的权限！");
                return;
            }
           
            gvList.DataSource = GetSource();
            gvList.DataBind();

        }
        //生成工资簿
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "SalaryTable.aspx", RightChar.Add))
            {
                JSUtility.Alert(this, "您没有增加工资的权限！");
                return;
            }
            string strYear = ddlYear.SelectedItem.Text.Trim();
            string strMonth = ddlMonth.SelectedItem.Text.Trim();
            string setsID = ddlSetsID.SelectedValue;
            if (setsID == "0")
            {
                JSUtility.Alert(this, "请先选择帐套");
                return;
            }
            if (salaryTableBiz.IsMonthEnd(strYear, strMonth, setsID))
            {
                JSUtility.Alert(this, "存在未月结工资簿，不能生成新工资簿");
                return;

            }
            
            if (salaryTableBiz.IsExistSLTable(strYear, strMonth, setsID))
            {
                JSUtility.Alert(this, "该月工资簿已经存在");
                return;
            }
            if (salaryTableBiz.CreateLists(strYear, strMonth, setsID))
            { JSUtility.Alert(this, "生成成功"); }
            else
            { JSUtility.Alert(this, "生成失败"); }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "SalaryTable.aspx", RightChar.Del))
            {
                JSUtility.Alert(this, "您没有删除工资的权限！");
                return;
            }
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            string setsID = ddlSetsID.SelectedValue;
            if (setsID == "0")
            {
                JSUtility.Alert(this, "请先选择帐套");
                return;
            }
            if (!salaryTableBiz.IsExistSLTable(strYear, strMonth, setsID))
            {
                JSUtility.Alert(this, "该月工资簿不存在");
                return;
            }
            if (salaryTableBiz.IsMonthEnd(strYear, strMonth, setsID))
            {
                JSUtility.Alert(this, "该月工资簿已经月结，不能删除");
                return;

            }
            if (salaryTableBiz.DeleteSLTable(strYear, strMonth, setsID))
            { JSUtility.Alert(this, "删除成功"); }
            else
            { JSUtility.Alert(this, "删除失败"); }
        }




        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "SalaryTable.aspx", RightChar.Add))
            {
                JSUtility.Alert(this, "您没有增加工资的权限！");
                return;
            }
            string empCode = txtSeWord.Text.Trim();
            string setsID = ddlSetsID.SelectedValue.ToString();
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            if (salaryTableBiz.IsMonthEnd(strYear, strMonth, setsID))
            {
                JSUtility.Alert(this, "该月工资簿已经月结或不存在，不能增加");
                return;

            }
            if (string.IsNullOrWhiteSpace(empCode))
            {
                Utils.JSUtility.Alert(this, "请选输入工号");
                return;
            }
            if (!new EmployeeBiz().IsExistEmpCode(empCode))
            {
                Utils.JSUtility.Alert(this, "该工号没有员工使用");
                return;
            }

            if (setsID == "" || setsID == "0")
            {
                Utils.JSUtility.Alert(this, "请选择帐套");
                return;
            }

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "js", @"<script language='javascript'>ShowLink('SalaryAdd.aspx?EmpCode=" + empCode + "&SetsID=" + setsID + "&StrYear=" + strYear + "&StrMonth=" + strMonth + "',780,610);</script>");


        }


        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            //{
            //    e.Row.Cells[0].Visible = false;
            //    e.Row.Cells[1].Visible = false;
            //}
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string empCode = e.Row.Cells[2].Text;
                string setsID = ddlSetsID.SelectedValue.ToString();
                string strYear = ddlYear.SelectedItem.Text;
                string strMonth = ddlMonth.SelectedItem.Text;
                e.Row.Cells[2].Text = " <a href=\"javascript:ShowLink('SalaryAdd.aspx?EmpCode=" + empCode + "&SetsID=" + setsID + "&StrYear=" + strYear + "&StrMonth=" + strMonth + "',780,610)\">" + empCode + "</a>";
            }



        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "SalaryTable.aspx", RightChar.Out))
            {
                JSUtility.Alert(this, "您没有导出工资的权限！");
                return;
            }
            string empCode = txtSeWord.Text.Trim();//只查询使用

            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            string setsID = ddlSetsID.SelectedValue;
            if (string.IsNullOrWhiteSpace(empCode))
            {
                if (setsID == "0")
                {
                    JSUtility.Alert(this, "请先选择帐套");
                    return;
                }
            }
            string salIndex="N";
            if (cbSalIndex.Checked == true) salIndex = "Y";
            DataTable resultDt = salaryTableBiz.GetLists(empCode, strYear, strMonth, setsID, salIndex);
            ExcelUtility.ExportDsToXls(this, resultDt, "SalaryTable.xls");
            //ExcelUtility.ExportDsToXls(this, salaryTableBiz.GetLists(empCode, strYear, strMonth, setsID), "SalaryTable.xls");
        }

        protected void btnBatch_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "SalaryTable.aspx", RightChar.Add))
            {
                JSUtility.Alert(this, "您没有增加工资的权限！");
                return;
            }
            string setsID = ddlSetsID.SelectedValue.ToString();
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            if (salaryTableBiz.IsMonthEnd(strYear, strMonth, setsID))
            {
                JSUtility.Alert(this, "该月工资簿已经月结或不存在，不能批量增加或减少发放工资");
                return;
            }
            if (setsID == "" || setsID == "0")
            {
                Utils.JSUtility.Alert(this, "请选择帐套");
                return;
            }
            Response.Redirect("SalaryBatch.aspx?SetsID=" + setsID + "&StrYear=" + strYear + "&StrMonth=" + strMonth );

        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvList.PageIndex = e.NewPageIndex;
            gvList.DataSource = GetSource();
            gvList.DataBind();
        }

     








    }
}